package com.cf.wim.controller;

import cn.hutool.core.util.NetUtil;
import com.cf.game.group.RelationService;
import com.cf.id.SnowflakeId;
import com.cf.location.City;
import com.cf.location.IPv4FormatException;
import com.cf.user.core.EnumUserType;
import com.cf.user.login.model.UserInfo;
import com.cf.user.login.service.TokenManager;
import com.cf.user.login.service.UserInfoService;
import com.cf.util.RequestUtil;
import com.cf.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * boot-awm-ImController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/9 下午 06:22
 */
@Slf4j
@Api(description = "临时会话")
@RequestMapping("/kf")
public class ImFrontController {

    @Autowired
    private SnowflakeId id;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RelationService relationService;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private City city;

    @ResponseBody
    @RequestMapping("/tempRelation")
    @ApiOperation(value = "获取临时会话token", notes = "获取临时会话token", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVo<Long> tempChat(
            @ApiParam(value = "客服Id", required = true) long kfId, HttpServletRequest request) {
        UserInfo kf = userInfoService.selectOne(kfId);
        UserInfo tempUser = userInfoService.insert(createTempInfo(RequestUtil.getIpAddr(request)));
        relationService.addTemporaryRelation(tempUser, kf);
        String token = tokenManager.registerToken(tempUser);
        return ResponseVo.success(token, "获取临时会话Id成功！");
    }

    private UserInfo createTempInfo(String ipAddr) {
        UserInfo userInfo = new UserInfo();
        long uId = id.nextId();
        userInfo.setId(uId);
        userInfo.setBirth(new Date());
        try {
            userInfo.setCity(Arrays.toString(city.find(ipAddr)));
        } catch (IPv4FormatException e) {
            userInfo.setCity("未知区域");
        }
        userInfo.setCountry("CN");
        userInfo.setName(getSelfTempName(ipAddr));
        userInfo.setPhone(uId + "");
        userInfo.setHead("http://doll-img.oss-cn-shanghai.aliyuncs.com/2017/12/34942974513119232.png");
        userInfo.setEmail(uId + "@temp.com");
        userInfo.setType(EnumUserType.TEMP.getCode());
        return userInfo;
    }


    /**
     * 获取自己的临时会话名字
     *
     * @param selfIp
     * @return
     */
    private String getSelfTempName(String selfIp) {
        if (NetUtil.isInnerIP(selfIp) || "0:0:0:0:0:0:0:1".equals(selfIp)) {
            return "内网测试用户";
        }
        String selfName = selfIp + "(";
        try {
            selfName += Arrays.toString(city.find(selfIp));
        } catch (IPv4FormatException e) {
            selfName += "未知地址";
            log.warn("获取ip地址信息失败！【{}】", selfIp, e);
        }
        selfName += ")";
        return selfName;
    }
}
